<?php
namespace app\home\controller;

use think\Request;
use think\Db;
use app\common\logic\Upload;
use app\common\logic\Agentlogic;

/**
 * Agent Controller
 */
class Agent extends Common
{
    private $agentLogic;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->agentLogic = new Agentlogic();
    }

	/**
	 * 地推管理
	 */
	public function manage()
    {
        $userInfo = $this->getLoginUserInfo();

        $realName = input('real_name');
        $phone = input('phone');

        $where = [
            'belong_agent' => $userInfo['session_id'],
        ];
        if ($realName) {
            $where['real_name'] = $realName;
        }

        if ($phone) {
            $where['phone'] = $phone;
        }

        $agents = Db::name('agent')->where($where)
            ->order('id', 'desc')
            ->select();

        foreach ($agents as &$item) {
            $item = $this->formatInfo($item);
        }

        return $this->fetch('manage', [
            'agents' => $agents
        ]);
    }

    /**
     * 添加地推页面
     */
    public function addPage()
    {
        return $this->fetch('add', [
        ]);
    }

    /**
     * 添加地推
     *
     * @return array
     */
    public function add()
    {
        $userInfo = $this->getLoginUserInfo();

        if ( Request::instance()->isPost()) {
            $data = input();

            $data['belong_agent'] = $userInfo['session_id'];
            $result = $this->agentLogic->addEmployee($data, true);

            if ($result['code'] == 0) {
                $this->exit_msg_callback('backFunc', $result['msg'], 1);
            } else {
                $this->exit_msg_callback('backFunc', $result['msg']);
            }

            //return $result;
        }
    }

    /**
     * 详情
     */
    public function detail()
    {
        $id = input('id');

        $agent = Db::name('agent')->where([
            'id' => $id
        ])->find();

        $agent = $this->formatInfo($agent);

        return $this->fetch('detail', [
            'agent' => $agent
        ]);
    }

    /**
     * 修改地推页面
     */
    public function putPage()
    {
        $id = input('id');

        $agent = Db::name('agent')->where([
            'id' => $id
        ])->find();

        $agent = $this->formatInfo($agent);

        return $this->fetch('putPage', [
            'agent' => $agent
        ]);
    }

    /**
     * 修改地推
     */
    public function put()
    {
        $userInfo = $this->getLoginUserInfo();
        if ( Request::instance()->isPost()) {
            $data = input();
            $data['belong_agent'] = $userInfo['session_id'];
            $result = $this->agentLogic->editEmployee($data);

            if ($result['code'] == 0) {
                $this->exit_msg_callback('backFunc', $result['msg'], 1);
            } else {
                $this->exit_msg_callback('backFunc', $result['msg']);
            }
        } else {
            $this->exit_msg_callback('backFunc', '非法操作');
        }
    }

    /**
     * 格式化信息
     *
     * @param $item
     * @return mixed
     */
    private function formatInfo($item)
    {
        switch ($item['is_use']) {
            case 1:
                $item['status'] = '正常';
                break;
            case 2:
                $item['status'] = '禁用';
        }

        $item['seller_count'] = Db::name('seller')->where([
            'salesman' => $item['id'],
        ])->count();

        $item['addtime'] = date('Y-m-d H:i:s', $item['addtime']);

        if(!empty($item['card_pic'])){
            $card_pic = '';
            $init_image = trim($item['card_pic'], ',');
            $img_list = explode(',', $init_image);
            $item['all_image'] = [];
            $url = config('txyunview');
            foreach ($img_list as $img) {
                $name = explode('/', $img);
                $name = array_pop($name);
                $item['all_image'][] = ['name' => $name, 'url' => $url.$img];
                $card_pic .= $url.$img.',';
            }
            $item['card_pic'] = trim($card_pic, ',');
        } else {
            $item['all_image'] = [];
        }

        $item['all_image'] = json_encode($item['all_image'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

        return $item;
    }

    /**
     * 上传图片
     */
    public function fileUpload()
    {
        $fileObj = new Upload();
        $dstPath = 'operator/Agent/';
        $res = $fileObj->upload('img_file', $dstPath, 1024*1024*10);
        if ($res['code'] == 0) {
            return json($res['msg']);
        } else {
            return json($res['msg']);
        }
    }
}

